package stage1.operation.module3;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

public class LandlordGame {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入任意键开始准备扑克！");
        sc.nextLine();
        ArrayList<String> poker = preparePoker();
        Collections.shuffle(poker);
        ArrayList<String> collation = preparePoker();
        System.out.println("扑克准备完成，洗牌结果如下！");
        System.out.println(poker);
        System.out.println("请输入任意键发牌！");
        sc.nextLine();
        distributePoker(collation, poker);
    }

    /**
     * 按斗地主规则生成对应牌
     *
     * @return
     */
    public static ArrayList<String> preparePoker() {
        ArrayList<String> poker = new ArrayList<>();
        poker.add("大王");
        poker.add("小王");

        String[] pokerSuit = {"黑桃♠", "红桃♥", "梅花♣", "方片♦"};
        String[] pokerNumber = {"2", "A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3"};

        for (int j = 0; j < pokerNumber.length; j++) {
            for (int i = 0; i < pokerSuit.length; i++) {
                poker.add(pokerSuit[i] + pokerNumber[j]);
            }
        }

        return poker;
    }

    /**
     * 摸牌，看牌过程
     *
     * @param collation
     * @param poKer
     */
    public static void distributePoker(ArrayList<String> collation, ArrayList<String> poKer) {
        ArrayList<String> player01 = draw(poKer, collation, 0);
        ArrayList<String> player02 = draw(poKer, collation, 1);
        ArrayList<String> player03 = draw(poKer, collation, 2);
        ArrayList<String> holeCard = draw(poKer, collation, 3);

        System.out.println("玩家1:" + player01);
        System.out.println("玩家1:" + player02);
        System.out.println("玩家1:" + player03);
        System.out.println("底牌:" + holeCard);
    }

    /**
     * 每个人摸牌
     *
     * @return
     */
    public static ArrayList<String> draw(ArrayList<String> poker, ArrayList<String> collation, int playerNum) {
        ArrayList<String> player = new ArrayList<>();
        ArrayList<String> holeCard = new ArrayList<>();

        for (int i = 0; i < poker.size(); i++) {
            String s = poker.get(i);
            if (i >= 51) {
                holeCard.add(s);
            } else if (i % 3 == playerNum) {
                player.add(s);
            } else if (i % 3 == playerNum) {
                player.add(s);
            } else if (i % 3 == playerNum) {
                player.add(s);
            }
        }
        Collections.sort(player, new Comparator() {
            @Override
            public int compare(Object o1, Object o2) {
                return collation.indexOf(o1) - collation.indexOf(o2);
            }
        });

        if (playerNum > 2) {
            return holeCard;
        }
        return player;
    }
}
